package com.qdd.freight.controller;


import com.qdd.freight.entity.Result;
import com.qdd.freight.enums.CacheKeyEnum;
import com.qdd.freight.util.RSAUtils;
import com.qdd.freight.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/getKey")
public class GetKeyController {

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 获取公钥方法
     */
    @GetMapping
    @ResponseBody
    public Result getKey() {
        Map<String, String> map = null;
        String publicKey = redisUtil.get(CacheKeyEnum.PUBLIC_KEY.getCacheKey(), String.class);
        if (publicKey == null) {
            Map<String, String> keyMap = RSAUtils.createKeys(1024);
            publicKey = keyMap.get("publicKey");
            String privateKey = keyMap.get("privateKey");
            redisUtil.set(CacheKeyEnum.PUBLIC_KEY.getCacheKey(), publicKey);
            redisUtil.set(CacheKeyEnum.PRIVATE_KEY.getCacheKey(), privateKey);
        }
        return Result.success("获取公钥", publicKey);
    }
}
